var app = getApp();



Page({
  data: {
    indexSelect: 0,
    circleList: 24, //圆点
   
    awardList: [], //奖品数组
    indexSelect: 0, //被选中的奖品index
    isRunning: false //是否正在抽奖
  },

  onLoad: function() {


    var _this = this
    // 请求会员等级
    var params = {

      sellerId: wx.getStorageSync('agent').id,
      userId: wx.getStorageSync('userId')
    }


    //请求抽奖列表
    app.HttpService.qingqjp(params).then(res => {
      console.log(res, "请求奖品列表")
      if(res.data.code==-200){
        //获奖提示
        wx.showModal({
          title: '恭喜您',
          content: res.data.msg,

          confirmColor: '#5677FC',
          showCancel: false,
          //去掉取消按钮
          success: (res) => {
            if (res.confirm) {
     
              this.setData({
                isRunning: false
              })
              wx.switchTab({
                url: '/pages/index/index'
              })
           
            }
          }
        })

      }else{
        this.setData({
          awardList: res.data.data.prizeList,
          prizeAwardPos: res.data.data.prizeAwardPos,
          prizeId: res.data.data.prizeId,
          lotteryBuyerId: res.data.data.lotteryBuyerId
        })
      }
   

    });










  },
  //获取随机数
  getRandom: function(u) {
    let rnd = Math.random() > 0.5 ? "2" : "1";
    u = u || 3;
    for (var i = 0; i < u; i++) {
      rnd += Math.floor(Math.random() * 10);
    }
    return Number(rnd);
  },

  //开始抽奖
  startDrawing: function() {
    // 请求会员等级
    var params = {
      sellerId: wx.getStorageSync('agent').id,
      userId: wx.getStorageSync('userId'),
      prizeId: this.data.prizeId,
      lotteryBuyerId: this.data.lotteryBuyerId


    }

    //请求抽奖意外
    app.HttpService.choujyw(params).then(res => {
      console.log(res, "请求意外情况")
      if (this.data.isRunning) return
      this.setData({
        isRunning: true
      })
      let indexSelect = 0
      let i = 0;
      let randomNum = this.getRandom(3);
      let timer = setInterval(() => {
        ++indexSelect;
        //这里用y=30*x+150函数做的处理.可根据自己的需求改变转盘速度

        indexSelect = indexSelect % 8;

        this.setData({
          indexSelect: indexSelect
        })
        i += 40;

        if (i > randomNum) {
          //去除循环
          clearInterval(timer)
          //指定那个下标中奖
          this.setData({
            indexSelect: this.data.prizeAwardPos
          })
          timer = null;
        
          if (res.data.code==-200){

            //获奖提示
            wx.showModal({
              title: '恭喜您',
              content: res.data.msg,

              confirmColor: '#5677FC',
              showCancel: false,
              //去掉取消按钮
              success: (res) => {
                if (res.confirm) {
               
                  this.setData({
                    isRunning: false
                  })
                  wx.switchTab({
                    url: '/pages/index/index'
                  })
                }
              }
            })
          }else{
            //获奖提示
            wx.showModal({
              title: '恭喜您',
              content: `获得了奖品【${this.data.awardList[this.data.prizeAwardPos]
                .awardName}】`,

              confirmColor: '#5677FC',
              showCancel: false,
              //去掉取消按钮
              success: (res) => {
                if (res.confirm) {
                  this.setData({
                    isRunning: false
                  })
                  wx.switchTab({
                    url: '/pages/index/index'
                  })
                }
              }
            })

          }


     




        }
      }, (70 + i))
    });
  }

})